﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using svCore.App;
using Vux.SVCore.Client.Network;
using Vux.SVCore.Delegate;

namespace svCore
{
    public partial class frmMain : Form
    {
        // Declare members
        private ClientListener m_client_listener;
        private UserProfile m_user_profile;

        public frmMain()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            m_user_profile = new UserProfile();
            m_user_profile.UpdateProfilePicture += new UpdateProfilePictureHandler(v_profile_form_UpdateProfilePicture);
            m_client_listener = new ClientListener(8989);
            m_client_listener.MsgNotify += new MsgNotifyHandler(m_socket_server_MsgNotify);
            m_client_listener.SocketError += new SocketErrorHandler(m_client_listener_SocketError);
            m_client_listener.FailRegisterPort += new FailRegisterPortHandler(m_client_listener_FailRegisterPort);
            m_client_listener.PictureData = m_user_profile.GetPictureData();
            m_client_listener.startServer();
        }

        private void m_client_listener_FailRegisterPort(object sender, int port)
        {
            if (InvokeRequired == true)
            {
                Invoke(new FailRegisterPortHandler(m_client_listener_FailRegisterPort), sender, port);
            }
            else
            {
                MessageBox.Show("Không thể mở cổng chờ: " + port + "\r\nLiên hệ quản trị viên để được hỗ trợ\r\nChương trình sẽ tự động thoát!", "Lỗi chương trình", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        public void m_client_listener_SocketError(object sender, Exception ex)
        {
            if (InvokeRequired == true)
            {
                Invoke(new SocketErrorHandler(m_client_listener_SocketError), sender, ex);
            }
            else
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void m_socket_server_MsgNotify(object sender, string msg)
        {
            if (InvokeRequired == true)
            {
                Invoke(new Vux.SVCore.Delegate.MsgNotifyHandler(m_socket_server_MsgNotify), sender, msg);
            }
            else
            {
                this.txtNotifyBox.Text += string.Format("\r\n{0}", msg);
                this.txtNotifyBox.Select(txtNotifyBox.Text.Length, 0);
                this.txtNotifyBox.ScrollToCaret();
            }
        }

        private void ChangeProfilePicture(byte[] ip_str_fname)
        {
            m_client_listener.PictureData = ip_str_fname;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                m_client_listener.stopListening();
                Console.WriteLine("Da tam dung svCore");
                Console.WriteLine("Chuong trinh giam sat ket thuc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void profilePictureTsMenu_Click(object sender, EventArgs e)
        {
            var v_profile_form = new frmProfile();
            v_profile_form.UserProfile = m_user_profile;            
            v_profile_form.ShowDialog();
        }

        private void v_profile_form_UpdateProfilePicture(byte[] ip_bmp_data)
        {
            ChangeProfilePicture(ip_bmp_data);
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {

        }

    }
}
